package com.stm.bi.model;

import com.alibaba.fastjson.JSONObject;
import com.stm.framework.annotation.Param;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 简介
 *
 * @author zhouyuanpeng
 * @date 2024-05-23 11:58
 */
public class DigestibilityData extends BaseOriginData {
    @Param(label = "销量")
    private BigDecimal qty;

    @Param(label = "库存")
    private BigDecimal inventory;

    @Param(label = "最后库存")
    private BigDecimal inventoryEnd;

    @Param(label = "累计库存")
    private BigDecimal cumulativeInventory;

    @Param(label = "累计销量")
    private BigDecimal cumulativeQty;

    @Param(label = "累计售罄率")
    private BigDecimal digestibility;


    @Override
    public void initData() {
//        累计售罄率为负数：赋0；
//        累计售罄率大于100%：赋0；
//        售罄率分母为0（库存与销量和为0）：赋0；
//        库存为空值：库存赋0；
//        如果售罄率小于等于0的，前端统一都显示：-
        if (this.inventory != null) {
            this.inventoryEnd = this.inventory;
        } else {
            this.inventoryEnd = BigDecimal.ZERO;
            this.inventory = BigDecimal.ZERO;
        }

        if (this.qty != null && this.qty.add(this.inventoryEnd).compareTo(BigDecimal.ZERO) != 0) {
            this.digestibility = this.qty.divide(this.qty.add(this.inventoryEnd), 6, RoundingMode.HALF_UP);
        } else {
            // 默认值
            this.digestibility = BigDecimal.ZERO;
        }

        if (this.digestibility.compareTo(BigDecimal.ZERO) < 0 || this.digestibility.compareTo(BigDecimal.ONE) > 0) {
            this.digestibility = BigDecimal.ZERO;
        }

        this.fieldValue1 = this.digestibility;
        super.initData();
    }

    @Override
    public void cumulativeData(JSONObject json, String totalType) {
        if (json != null) {
            DigestibilityData obj = JSONObject.toJavaObject(json, DigestibilityData.class);
            // 累计销量
            if (obj.getQty() != null) {
                this.cumulativeQty = obj.getQty();
            }
            if (obj.getInventory() != null) {
                this.cumulativeInventory = obj.getInventory();
            }
            // 累计退货率 = 累计退货量 / 累计总销量
            if ("date".equals(totalType)) {
                if (this.cumulativeQty != null && this.cumulativeQty.add(this.inventory).compareTo(BigDecimal.ZERO) != 0) {
                    this.digestibility = this.cumulativeQty.divide(this.cumulativeQty.add(this.inventory), 6, RoundingMode.HALF_UP);
                } else {
                    // 默认值
                    this.digestibility = BigDecimal.ZERO;
                }
            } else {
                if (this.cumulativeQty != null && this.cumulativeQty.add(this.inventoryEnd).compareTo(BigDecimal.ZERO) != 0) {
                    this.digestibility = this.cumulativeQty.divide(this.cumulativeQty.add(this.inventoryEnd), 6, RoundingMode.HALF_UP);
                } else {
                    // 默认值
                    this.digestibility = BigDecimal.ZERO;
                }
            }

            if (this.digestibility.compareTo(BigDecimal.ZERO) < 0 || this.digestibility.compareTo(BigDecimal.ONE) > 0) {
                this.digestibility = BigDecimal.ZERO;
            }

            this.fieldValue1 = this.digestibility;
            super.initData();
        }
    }


    @Override
    public void totalQty(JSONObject json, String totalType) {
        DigestibilityData obj = JSONObject.toJavaObject(json, DigestibilityData.class);
        // 累计销量
        if (obj.getQty() != null) {
            this.qty = this.qty == null ? obj.getQty() : this.qty.add(obj.getQty());
        }
        if (obj.getInventory() != null) {
            this.inventory = this.inventory == null ? obj.getInventory() : this.inventory.add(obj.getInventory());
            this.inventoryEnd = obj.getInventory();
        }
        // 累计退货率 = 累计退货量 / 累计总销量
        if ("date".equals(totalType)) {
            if (obj.getCumulativeQty() != null) {
                this.cumulativeQty = this.cumulativeQty == null ? obj.getCumulativeQty() : this.cumulativeQty.add(obj.getCumulativeQty());
            }
        }

        if (this.qty != null && this.qty.add(this.inventoryEnd).compareTo(BigDecimal.ZERO) != 0) {
            this.digestibility = this.qty.divide(this.qty.add(this.inventoryEnd), 6, RoundingMode.HALF_UP);
        } else {
            // 默认值
            this.digestibility = BigDecimal.ZERO;
        }

        if (this.digestibility.compareTo(BigDecimal.ZERO) < 0 || this.digestibility.compareTo(BigDecimal.ONE) > 0) {
            this.digestibility = BigDecimal.ZERO;
        }

        this.fieldValue1 = this.digestibility;
        super.initData();
    }

    public BigDecimal getQty() {
        return qty;
    }

    public DigestibilityData setQty(BigDecimal qty) {
        this.qty = qty;
        return this;
    }

    public BigDecimal getInventory() {
        return inventory;
    }

    public DigestibilityData setInventory(BigDecimal inventory) {
        this.inventory = inventory;
        return this;
    }

    public BigDecimal getInventoryEnd() {
        return inventoryEnd;
    }

    public DigestibilityData setInventoryEnd(BigDecimal inventoryEnd) {
        this.inventoryEnd = inventoryEnd;
        return this;
    }

    public BigDecimal getCumulativeQty() {
        return cumulativeQty;
    }

    public DigestibilityData setCumulativeQty(BigDecimal cumulativeQty) {
        this.cumulativeQty = cumulativeQty;
        return this;
    }

    public BigDecimal getDigestibility() {
        return digestibility;
    }

    public DigestibilityData setDigestibility(BigDecimal digestibility) {
        this.digestibility = digestibility;
        return this;
    }

    public BigDecimal getCumulativeInventory() {
        return cumulativeInventory;
    }

    public DigestibilityData setCumulativeInventory(BigDecimal cumulativeInventory) {
        this.cumulativeInventory = cumulativeInventory;
        return this;
    }
}
